<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    
     <div class="container-div">
		<div class="row">
			<div class="col-sm-12 search-collapse">
				<form id="formId">
					<div class="select-list">
						<ul>
							<li>
								子节点名称：<input type="text" name="nodeName"/>
							</li>

							<li>
								节点端口：<input type="text" name="nodePort"/>
							</li>

							<li>
								ip地址：<input type="text" name="ip"/>
							</li>

							<li>
								Jmeter链接端口号：<input type="text" name="jmeterPort"/>
							</li>

							<li>
								节点机用户名：<input type="text" name="userName"/>
							</li>


							<li>
								linux | windows：<input type="text" name="os"/>
							</li>

							<li>
								节点的Jmeter路径：<input type="text" name="homeDir"/>
							</li>

							<li>
								状态  0：禁用   1：正常：<input type="text" name="status"/>
							</li>


							<li>
								<a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
								<a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
							</li>
						</ul>
					</div>
				</form>
			</div>
			
	        <div class="btn-group-sm hidden-xs" id="toolbar" role="group">
<!--				<a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="jmeter:jmeterNode:add">-->
<!--					<i class="fa fa-plus"></i> 添加-->
<!--				</a>-->
				<a class="btn btn-primary btn-edit disabled" onclick="$.operate.edit()" shiro:hasPermission="jmeter:jmeterNode:edit">
					<i class="fa fa-edit"></i> 修改
				</a>
				<a class="btn btn-danger btn-del btn-del disabled" onclick="$.operate.removeAll()" shiro:hasPermission="jmeter:jmeterNode:remove">
					<i class="fa fa-remove"></i> 删除
				</a>
				<a class="btn btn-primary btn-del btn-del disabled" onclick="startJmeter()" shiro:hasPermission="jmeter:jmeterNode:start">
					<i class="fa fa-play"></i> 启动Jmeter
				</a>
				<a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="jmeter:jmeterNode:export">
						<i class="fa fa-download"></i> 导出
				 </a>
			</div>
			<div class="col-sm-12 select-table table-striped">
				<table id="bootstrap-table" data-mobile-responsive="true"></table>
			</div>
		</div>
	</div>
    <div th:include="include :: footer"></div>
    <script th:inline="javascript">
        var editFlag = [[${@permission.hasPermi('jmeter:jmeterNode:edit')}]];
        var removeFlag = [[${@permission.hasPermi('jmeter:jmeterNode:remove')}]];
        var prefix = ctx + "jmeter/jmeterNode";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
				exportUrl: prefix + "/export",
                modalName: "性能测试分布式节点",
				search: false,
		        showExport: true,
                columns: [{
		            checkbox: true
		        },
				{
					field : 'id', 
					title : '',
					visible: false
				},
				{
					field : 'nodeName', 
					title : 'Jmeter节点名称',
					sortable: true
				},
				{
					field: 'status',
					title: '客户端状态',
					width: '10%',
					class: 'myTDLengthHidden',
					formatter: function (value,
										 row, index) {
						if (value == 1) {
							var hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon glyphicon-ok-sign" style="color: rgb(0, 175, 0);"> 状态正常</span>';
						} else if (value == 0) {
							var hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon glyphicon-minus-sign" style="color: rgb(199, 0, 0);"> 状态异常</span>';
						} else if (value == -1) {
							var hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon glyphicon-minus-sign" style="color: rgba(1,6,14,0.96);"> 禁用</span>';
						} else {
							var hdiv = '<span id="statusSpan' + row.id + '" class="glyphicon glyphicon-question-sign" style="color: rgb(255, 168, 0); font-size: 14px;"> 状态未知</span>';
						}
						refreshClientStatus(row.id);
						return '<font id="statusFont' + row.id + '">' + hdiv + '</font>';
					}
				},
				{
					field : 'nodePort', 
					title : '节点端口',
					sortable: true
				},
				{
					field : 'ip', 
					title : 'ip地址',
					sortable: true
				},
				{
					field : 'jmeterPort', 
					title : 'Jmeter链接端口号',
					sortable: true
				},
				{
					field : 'userName', 
					title : '节点机用户名',
					sortable: true
				},
				{
					field : 'passwd', 
					title : '节点机密码',
					sortable: true
				},
				{
					field : 'maxConcurrency', 
					title : '最大并发数 ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'cpu', 
					title : 'cpu ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'memory', 
					title : '内存 单位g ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'disk', 
					title : '磁盘 单位g  ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'io', 
					title : 'io ',
					formatter:function(v,r,i){
						return v==-1?'-':v;
					},
					sortable: true
				},
				{
					field : 'os', 
					title : 'linux | windows',
					sortable: true
				},
				{
					field : 'homeDir', 
					title : '子节点的Jmeter路径',
					sortable: true
				},
				{
					field : 'addTime', 
					title : '创建时间',
					sortable: true
				},
				{
					field : 'addBy', 
					title : '提交用户id',
					sortable: true
				},
				{
					field : 'updateTime', 
					title : '修改时间',
					sortable: true
				},
				{
					field : 'updateBy', 
					title : '修改用户id',
					sortable: true
				},
		        {
		            title: '操作',
		            align: 'center',
		            formatter: function(value, row, index) {
		            	var actions = [];
		            	actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="#" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="#" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a>');
						return actions.join('');
		            }
		        }]
            };
            $.table.init(options);
        });

		/*刷新客户端状态*/
		function refreshClientStatus(clientId) {
			var url = prefix + "/getClientStatusByClientId/" + clientId;
			$.getJSON(url, null, function call(result) {
				if (result.status == 1) {
					/*删除老的span*/
					var oldSpan = document.getElementById("statusSpan" + clientId);
					oldSpan.parentNode.removeChild(oldSpan);
					/*新建一个待更新span*/
					var updateSpan = document.createElement("span");
					updateSpan.innerText = "状态正常"
					updateSpan.setAttribute("class", "glyphicon glyphicon-ok-sign");
					updateSpan.setAttribute("style", "color: rgb(0, 175, 0)");
					updateSpan.setAttribute("id", "statusSpan" + clientId);
					document.getElementById("statusFont" + clientId).appendChild(updateSpan);
				} else if (result.status == 0) {
					/*删除老的span*/
					var oldSpan = document.getElementById("statusSpan" + clientId);
					oldSpan.parentNode.removeChild(oldSpan);
					/*新建一个待更新span*/
					var updateSpan = document.createElement("span");
					updateSpan.innerText = "状态异常"
					updateSpan.setAttribute("class", "glyphicon glyphicon-minus-sign");
					updateSpan.setAttribute("style", "color: rgb(199, 0, 0);");
					updateSpan.setAttribute("id", "statusSpan" + clientId);
					document.getElementById("statusFont" + clientId).appendChild(updateSpan);
				} else if (result.status == -1) {
					/*删除老的span*/
					var oldSpan = document.getElementById("statusSpan" + clientId);
					oldSpan.parentNode.removeChild(oldSpan);
					/*新建一个待更新span*/
					var updateSpan = document.createElement("span");
					updateSpan.innerText = "Jmeter已关闭"
					updateSpan.setAttribute("class", "glyphicon glyphicon-question-sign");
					updateSpan.setAttribute("style", "color: rgba(1,6,14,0.96);");
					updateSpan.setAttribute("id", "statusSpan" + clientId);
					document.getElementById("statusFont" + clientId).appendChild(updateSpan);
				}
			});
			setTimeout("refreshClientStatus(" + clientId + ")", 5000);
		}


		function startJmeter() {
			let arr=$('#bootstrap-table').bootstrapTable('getSelections');
			if(arr.length==0){
				$.modal.alertWarning("请至少选择一条记录");
				return;
			}

			let disabled=arr.filter(it=>it.status==-1);
			if(disabled.length==0){
				$.modal.alertWarning("没有需要启动的客户端");
				return;
			}

			var idlist=disabled.map(it=>it.id);

			var config = {
				url: prefix + "/startJmeters",
				type: "post",
				dataType: "json",
				contentType:"application/json",
				data: JSON.stringify(idlist),
				beforeSend: function () {
				},
				success: function(result) {

					window.parent.$.modal.msgSuccess(result.msg);
					window.parent.$.table.refresh();

					$.modal.close();
					window.parent.$.modal.msgSuccess(result.msg);
					window.parent.$.treeTable.refresh();
					$.modal.closeLoading();
					$.modal.enable();
				}
			};
			$.ajax(config);
		}

    </script>
</body>
</html>